package web.controller.employer;

import common.consts.ContentType;
import common.validation.ValidationResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import service.ActiveCouponService;
import service.ServiceResponse;
import web.Routes;
import web.controller.Base;
import web.req.CouponRequest;
import web.security.Protection;

/**
 * Created by aceyin on 15/6/27.
 */
@RestController
@RequestMapping(Routes.Employer.COUPON)
public class Coupon extends Base {
    private static final Logger LOGGER = LoggerFactory.getLogger(Coupon.class);

    @Autowired
    private ActiveCouponService activeCouponService;

    /**
     * 获取一个商家有效的Coupon
     *
     * @return
     */
    @Protection
    @RequestMapping(method = RequestMethod.GET, produces = ContentType.JSON)
    public ServiceResponse get() {
        long employer = getUserIdFromAuthHeader();
        return activeCouponService.getMyActiveCoupons(employer);
    }

    /**
     * 激活一个Coupon
     *
     * @param request
     * @return
     */
    @Protection
    @RequestMapping(method = RequestMethod.POST, produces = ContentType.JSON)
    public ServiceResponse active(@RequestBody CouponRequest request) {
        ValidationResult result = request.validate();
        if (result.isFailed()) {
            return fail(result.getMessage());
        }

        long employer = getUserIdFromAuthHeader();
        return activeCouponService.activeCoupon(employer, request.getCode());
    }
}
